package com.qc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qc.servicebase.config.GuliException;
import com.qc.serviceedu.entity.EduChapter;
import com.qc.serviceedu.entity.EduVideo;
import com.qc.serviceedu.entity.chapter.ChapterVo;
import com.qc.serviceedu.entity.chapter.VideoVo;
import com.qc.serviceedu.entity.vo.OneSubjectVo;
import com.qc.serviceedu.entity.vo.TwoSubjectVo;
import com.qc.serviceedu.mapper.EduChapterMapper;
import com.qc.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author AltmanQc
 * @since 2020-12-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        //1.根据课程id查出课程的所有章节
        QueryWrapper<EduChapter> chapterVoQueryWrapper = new QueryWrapper<>();
        chapterVoQueryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = this.baseMapper.selectList(chapterVoQueryWrapper);
        //2.根据课程id查出课程的所有小结
        QueryWrapper<EduVideo> VideoVoQueryWrapper = new QueryWrapper<>();
        chapterVoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> videoList= this.videoService.list(VideoVoQueryWrapper);
        //3.遍历章节的list集合进行封装
        //创建List集合，用于存储最终的封装
        List<ChapterVo> finalChapterList = new ArrayList<>();
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter chapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalChapterList.add(chapterVo);
            //4.遍历小结的list集合进行封装
            List<VideoVo> finalVideoList = new ArrayList<>();
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo video = videoList.get(j);
                //判断二级分类course_id和一级分类是否一样
                if (video.getChapterId().equals(chapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    finalVideoList.add(videoVo);
                }
                //把下面的二级分类放到二级分类
                chapterVo.setChildren(finalVideoList);
            }
        }
        return finalChapterList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //1.查询当前删除的章节是否有小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(queryWrapper);
        if (count>0){
            throw new GuliException(20001,"含有小节，不能删除");
        }else {
            int delete = this.baseMapper.deleteById(chapterId);
            return delete>0;
        }
    }

    //1.根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
